<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>修改</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/js/layui/css/layui.css" media="all">
    <style type="text/css">
        .uploader-list {
            margin-left: -15px;
        }

        .uploader-list .info {
            position: relative;
            margin-top: -25px;
            background-color: black;
            color: white;
            filter: alpha(Opacity=80);
            -moz-opacity: 0.5;
            opacity: 0.5;
            width: 200px;
            height: 25px;
            text-align: center;
            display: none;
        }

        .uploader-list .handle {
            position: relative;
            background-color: black;
            color: white;
            filter: alpha(Opacity=80);
            -moz-opacity: 0.5;
            opacity: 0.5;
            width: 200px;
            text-align: right;
            height: 18px;
            margin-bottom: -18px;
            display: none;
        }

        .uploader-list .handle i {
            margin-right: 5px;
        }

        .uploader-list .handle i:hover {
            cursor: pointer;
        }

        .uploader-list .file-iteme {
            margin: 12px 0 0 15px;
            padding: 1px;
            float: left;
        }
        .layui-form-select dl{
            z-index: 10000;
        }
    </style>
    <script src="../js/verify.js"></script>
</head>
<body>
<div style="width:80%;margin:15px auto;overflow:auto;" id="aaa">
    <form class="layui-form" action="">
        <input type="hidden" class="input-text" id="nsid" name="nsid">
        <div class="layui-form-item">
            <label class="layui-form-label">新闻分类</label>
            <div class="layui-input-block">
                <select lay-verify="required" lay-search id="tpid" name="tpid">
                    <option value="">请选择</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">新闻标题</label>
            <div class="layui-input-block">
                <input type="text" name="nstitle" id="nstitle" required  lay-verify="required" placeholder="请输入名称" autocomplete="off" class="layui-input">
            </div>
        </div>
<!--        <div class="layui-form-item">-->
<!--            <label class="layui-form-label">新闻副标题</label>-->
<!--            <div class="layui-input-block">-->
<!--                <input type="text" name="nssubtitle" id="nssubtitle" required  lay-verify="required" placeholder="请输入名称" autocomplete="off" class="layui-input">-->
<!--            </div>-->
<!--        </div>-->
        <div class="layui-form-item">
            <label class="layui-form-label">是否显示</label>
            <div class="layui-input-block">
                <select lay-verify="required" lay-search id="nsstatus" name="nsstatus" lay-filter="is_show">
                    <option value="1">是</option>
                    <option value="0">否</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item m-select-item">
            <label class="layui-form-label">是否热门</label>
            <div class="layui-input-block">
                <select lay-verify="required" lay-search id="status" name="status">
                    <option value="1">是</option>
                    <option value="0">否</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item m-select-item">
            <label class="layui-form-label">是否显示轮播图位置</label>
            <div class="layui-input-block">
                <select lay-verify="required" lay-search id="showindex" name="showindex">
                    <option value="1">是</option>
                    <option value="0">否</option>
                </select>
            </div>
        </div>

        <div id="texts">
            <label class="layui-form-label">新闻内容</label>
            <div class="layui-input-block">
                <script id="editor_tai" type="textain" style="height: 200px;width:98%;margin: auto"></script>
            </div>
        </div>

        <div class="layui-upload" style="margin-top: 20px">
            <button type="button" class="layui-btn" id="test" style="display: inline;float: left;">上传图片</button>
            <div class="layui-upload-list" style="display: inline;margin-left: 30px;">
                <img class="layui-upload-img" name="image" id="image" style="width: 200px;height: 150px">
                <p id="demoText"></p>
            </div>
            <input type="hidden" class="input-text" id="nsimg" name="nsimg">
        </div>

        <div class="layui-form-item" style="margin-top: 20px">
            <div class="layui-input-block">
                <button style="width: 100px;" class="layui-btn" id="updCountry" lay-submit lay-filter="updCountry">立即修改</button>
                <button style="width: 100px;" type="reset" id="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>


<!--<script type="text/html" id="tb">-->
<!--    <div class="layui-container" style="margin-left:0px;">-->
<!--        <a class="layui-btn layui-btn-xs" lay-event="add"><i class="layui-icon layui-icon-addition"></i>新增内容</a>-->
<!--    </div>-->
<!--</script>-->
<script src="/js/layui/layui.all.js"></script>
<script src="/js/ueditor/1.4.3/ueditor.config.js" type="text/javascript" charset="utf-8"></script>
<script src="/js/ueditor/1.4.3/ueditor.all.js" type="text/javascript" charset="utf-8"></script>
<script>

    //url 传参
    function GetQueryString(name){
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var a = decodeURI(decodeURI(window.location.search));
        var r = a.substr(1).match(reg);//search,查询？后面的参数，并匹配正则
        if(r!=null)return  unescape(r[2]); return null;
    }

    var ue = UE.getEditor('editor_tai');


    layui.use(['form' , 'jquery' , 'upload', 'layedit', 'table'], function(){
        var form = layui.form;
        var $ = layui.jquery;
        var upload = layui.upload;
        var table = layui.table;
        id = GetQueryString("nsid");
        $("#nsid").val(id);
        layer.load(2);
        $.ajax({
            url: "/newtype/newtypeAll"
            , success: function (res) {
                if (res.code == 0) {
                    layer.closeAll();
                    // 下拉列表添加数据,再渲染
                    $.each(res.data, function (index, item) {
                        $("select[name = 'tpid']").append(new Option(item.name, item.id,));
                    });
                    $("#tpid").attr("disabled", false);

                    $.ajax({
                        url: "/news/getnewsById?nsid="+id,
                        type: 'get',
                        success: function (result) {
                            layer.closeAll();
                            if (result.code == 200) {
                                $("#tpid").val(result.data.tpid);
                                form.render("select");
                                $("#nsimg").val(result.data.nsimg);
                                $("#image").attr("src", "/upload/"+result.data.nsimg);
                                $("#nstitle").val(result.data.nstitle);
                                $("#nssubtitle").val(result.data.nssubtitle);
                                $("#nsstatus").val(result.data.nsstatus);
                                $("#showindex").val(result.data.showindex);
                                $("#status").val(result.data.status);
                                if(result.data.nsstatus == 0){
                                    $(".m-select-item").css("display", "none");
                                }
                                $("#editor_tai").val(result.data.nscontent);
                                if(result.data.nscontent != null && result.data.nscontent != undefined) {

                                    if(ue.isReady == 1) {
                                        ue.setContent(result.data.nscontent);
                                    }else {
                                        ue.addListener("ready", function () {
                                            //赋值
                                            ue.setContent(result.data.nscontent);
                                        })
                                    }// 上面的UEditor对象准备好之后，下面才可以使用。

                                }
                                form.render();
                            }
                        }
                    })
                } else if (res.code == 1) {
                    layer.msg(res.msg);
                } else {
                    layer.msg('拒绝访问');
                }
            }
            , error: function () {
                layer.closeAll();
                console.log("ajax error");
            }
        });

        form.on("select(is_show)" , function(data) {
            if(data.value * 1 == 0){
                $(".m-select-item").css("display", "none");
            }else{
                $(".m-select-item").css("display", "block");
            }
            form.render()
        })

        //编辑操作
        form.on('submit(updCountry)',function(data){
            // var nsimg = $("input[name='nsimg']").val();
            // data.field.nsimg = nsimg;
            if (data.field.nsimg =="" || data.field.nsimg == null){
                layer.msg("请上传图片",{icon:2,time:2000});
                return false;
            }
            if (ue.getContent() == null || ue.getContent() == "" || ue.getContent() == undefined) {
                layer.msg('新闻内容不能为空');
                return false;
            }
            data.field.nscontent = ue.getContent();
            data.field.nstitle =  replaceReg(data.field.nstitle);  // 替换
            $.ajax({
                url: "/news/updae_news"
                , type: "POST"
                , contentType: 'application/json'
                , data: JSON.stringify(data.field)
                , success: function (data) {
                    // console.log(res);
                    if (data.code == 200) {
                        layer.close(layer.index);
                        layer.msg('修改成功');
                        setTimeout(function () {
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);//关闭弹出层
                            parent.location.reload();//重新加载父页面表格
                        }, 2000);
                    } else {
                        layer.msg(data.msg,{icon:2,time:1500});
                    }
                }
                , error: function () {
                    console.log("ajax error");
                }
            });
            layer.closeAll();
            return false;
        });

        //logo上传
        var uploadInst = upload.render({
            elem: '#test'
            , url: '/upload' //改成您自己的上传接口
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#image').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //判断上传是否成功
                if (res.code == 0) {
                    // image = res.data
                    $("#nsimg").val(res.data);
                    return layer.msg('上传成功！');
                } else {
                    return layer.msg('上传失败！');
                }
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #ff5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        })
    });
</script>
</body>
</html>